<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="goodsInfor.query"
            class="input-with-select"
            clearable
            @clear="getDataList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getDataList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="addShop">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格部分 -->
      <el-table
        :data="dataList"
        style="width: 100%"
        border
        stripe
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="540"
        >
        </el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
        >
        </el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
        >
        </el-table-column>
        <el-table-column
          prop="add_time"
          label="创建时间"
          width="200"
        >
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
							@click="editClick(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeData(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页部分 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="goodsInfor.pagenum"
        :page-sizes="[5, 15, 20, 25]"
        :page-size="goodsInfor.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getData,deleteData} from "network/goodsList";
export default {
  data() {
    return {
      goodsInfor: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },

      dataList: [],
			total: null,
			
			editList : []
    };
  },
  methods: {
    getDataList() {
      getData("/goods", this.goodsInfor).then(res => {
        if (res.data.meta.status === 200) {
          this.dataList = res.data.data.goods;
          this.total = res.data.data.total;
          // console.log(this.dataList);
        } else {
          this.$Message.error("数据获取失败！");
        }
      });
    },
    handleSizeChange(size) {
      this.goodsInfor.pagesize = size;
      this.getDataList();
    },
    handleCurrentChange(num) {
      this.goodsInfor.pagenum = num;
      this.getDataList();
    },
    async removeData(id) {
      const isTrue = await this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
			}).catch(err => err)
			console.log(isTrue)
			if(isTrue === 'cancel'){
				this.$Message.info('取消删除！')
			}else{
				deleteData(`goods/${id}`).then(res => {
					console.log(res)
					if(res.data.meta.status === 200){
						this.getDataList()
						this.$Message.success('删除成功！')
					}
				})
			}
		},
		editClick(id) {
			getData(`goods/${id}`).then(res => {
				if(res.data.meta.status === 200){


					// 数据获取到了，之后补充


					this.editList = res.data.data
					// console.log(this.editList)
				}else{
					this.$Message.error('数据获取失败！')
				}
			})
		},

		addShop(){
			this.$router.push('/goods/add')
		}
  },
  created() {
    this.getDataList();
  }
};
</script>
<style lang="less" scoped>
.el-breadcrumb {
  padding-bottom: 20px;
}
.el-table {
  margin-top: 15px;
}
</style>